package com.lagou.springbootblog.controller;

import com.lagou.springbootblog.bean.PageParam;
import com.lagou.springbootblog.domain.Article;
import com.lagou.springbootblog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Calendar;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping("/index")
    public String index(Model model, PageParam pageParam){
        Page<Article> page = articleService.getArticlesPage(pageParam, null);
        model.addAttribute("page", page);
        model.addAttribute("articles", page.getContent());
        model.addAttribute("pages", page.getTotalPages());
        System.out.println("文章总数：" + page.getTotalElements() + " , 总页数: " + page.getTotalPages());
        return "client/index";
    }

    @RequestMapping("/test")
    public String test(Model model){
        System.out.println("test view");
        List<Article> articleList = articleService.findAll();
        for (Article article : articleList) {
            System.out.println(article);
        }
        return "test";
    }
}
